package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ author     ：廖传港
 * @ date       ：Created in 2020/12/29 18:44
 * @ description：
 * @ modified By：
 * @ ersion:
 */
@Api("商品详情汇总")
@RequestMapping("/api/item")
@RestController
public class ItemController {

    @Autowired
    private ProductFeignClient productFeignClient;

    //Spring容器
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;//必须实例化，也就是要写ThreadConfig.java文件

    @GetMapping("/{skuId}")
    public Map getItem(@PathVariable Long skuId) {

        Map map = new HashMap();


        CompletableFuture<SkuInfo> skuInfo1 = CompletableFuture.supplyAsync(() -> {
            //1:skuInfo
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        CompletableFuture<Void> categoryViewCompletableFuture = skuInfo1.thenAcceptAsync((skuInfo) -> {
            //2:三级分类
            BaseCategoryView baseCategoryView = productFeignClient.
                    getBaseCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);


        CompletableFuture<BigDecimal> priceCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //3:价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
            return skuPrice;
        }, threadPoolExecutor);


        CompletableFuture<Void> spuSaleAttrList = skuInfo1.thenAcceptAsync((skuInfo) -> {
            //4:销售属性及属性值
            List<SpuSaleAttr> spuSaleAttrAndChecked = productFeignClient.
                    getSpuSaleAttrAndChecked(skuInfo.getSpuId(), skuId);
            map.put("spuSaleAttrList", spuSaleAttrAndChecked);
        }, threadPoolExecutor);


        CompletableFuture<Void> valuesSkuJson = skuInfo1.thenAcceptAsync((skuInfo) -> {
            //5:组合 对应 skuId    json串
            Map saleAttrValuesBySpu = productFeignClient.getSaleAttrValuesBySpu(skuId);
            map.put("valuesSkuJson", JSONObject.toJSONString(saleAttrValuesBySpu));
        }, threadPoolExecutor);

        //TODO 6:远程调用ES 更新热度

        CompletableFuture.allOf(
                skuInfo1,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuSaleAttrList,
                valuesSkuJson
        ).join();
        return map;

    }
}
